package com.smartcruise.report.reportRecord;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.data.ReportData;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.report.EditType;
import com.smartcruise.report.ReportDataEditActivity;
import com.smartcruise.util.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.OrderedRealmCollection;
import io.realm.RealmBaseAdapter;

/**
 * Created by FengChaoQun
 * on 2018/1/19 0019
 * 通报记录adapter
 */

public class ReportAdapter extends RealmBaseAdapter<ReportData> {

    private Context context;

    public ReportAdapter(@Nullable OrderedRealmCollection<ReportData> data, Context context) {
        super(data);
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_report, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final ReportData reportData = getItem(position);
        viewHolder.title.setText(reportData.getName());
        viewHolder.time.setText(TimeUtils.getSimpleTimeString(reportData.getCreateTime()));
        if (reportData.isFromNet()) {
            if (reportData.isAuditSuccess()) {
                viewHolder.status.setText("已处理");
            } else if (CruiseContract.AUDIT_FAIL.equals(reportData.getAuditResult())) {
                switch (reportData.getUploadStatus()) {
                    case 0:
                        viewHolder.status.setText("被驳回");
                        break;
                    case 1:
                        viewHolder.status.setText("(被驳回)已上传概要数据");
                        break;
                    case 2:
                        viewHolder.status.setText("(被驳回)已上传");
                        break;
                }
            } else {
                viewHolder.status.setText("已上传");
            }
        } else {
            switch (reportData.getUploadStatus()) {
                case 0:
                    viewHolder.status.setText("编辑");
                    break;
                case 1:
                    viewHolder.status.setText("已上传概要数据");
                    break;
                case 2:
                    viewHolder.status.setText("已上传");
                    break;
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReportDataEditActivity.start(context, reportData.isAuditSuccess() ? EditType.VIEW : EditType.MODIFY, reportData.getId());
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.status)
        TextView status;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
